
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>爬虫实战 · Dart编程指南</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="arcticfox">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="HTTP服务器.html" />
    
    
    <link rel="prev" href="网络编程.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    
    
        
        <li>
            <a href="http://www.bczl.xyz/" target="_blank" class="custom-link">主页</a>
        </li>
    
    

    
    <li class="divider"></li>
    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础篇.html">
            
                <a href="基础篇.html">
            
                    
                    基础篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="环境搭建.html">
            
                <a href="环境搭建.html">
            
                    
                    环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="简单语法.html">
            
                <a href="简单语法.html">
            
                    
                    简单语法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="进阶篇.html">
            
                <a href="进阶篇.html">
            
                    
                    进阶篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="面向对象.html">
            
                <a href="面向对象.html">
            
                    
                    面向对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="泛型.html">
            
                <a href="泛型.html">
            
                    
                    泛型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="枚举.html">
            
                <a href="枚举.html">
            
                    
                    枚举
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="库与导入.html">
            
                <a href="库与导入.html">
            
                    
                    库与导入
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="异步编程.html">
            
                <a href="异步编程.html">
            
                    
                    异步编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.5.1" data-path="异步编程之Future.html">
            
                <a href="异步编程之Future.html">
            
                    
                    Future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.2" data-path="异步编程之Stream.html">
            
                <a href="异步编程之Stream.html">
            
                    
                    Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5.3" data-path="异步编程之Isolate.html">
            
                <a href="异步编程之Isolate.html">
            
                    
                    Isolate
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理与 Zone
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.7" data-path="生成器.html">
            
                <a href="生成器.html">
            
                    
                    生成器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.8" data-path="标准流与文件.html">
            
                <a href="标准流与文件.html">
            
                    
                    标准流与文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.9" data-path="运算符.html">
            
                <a href="运算符.html">
            
                    
                    附：运算符
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="常用标准库.html">
            
                <a href="常用标准库.html">
            
                    
                    常用标准库
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="集合.html">
            
                <a href="集合.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="URI处理.html">
            
                <a href="URI处理.html">
            
                    
                    URI处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="日期和时间.html">
            
                <a href="日期和时间.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="环境及外部调用.html">
            
                <a href="环境及外部调用.html">
            
                    
                    环境及外部调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="其他.html">
            
                <a href="其他.html">
            
                    
                    其他
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="实战篇.html">
            
                <a href="实战篇.html">
            
                    
                    实战篇
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="网络编程.html">
            
                <a href="网络编程.html">
            
                    
                    网络编程
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5.2" data-path="爬虫实战.html">
            
                <a href="爬虫实战.html">
            
                    
                    爬虫实战
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="HTTP服务器.html">
            
                <a href="HTTP服务器.html">
            
                    
                    HTTP服务器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="WebSocket服务器.html">
            
                <a href="WebSocket服务器.html">
            
                    
                    WebSocket服务器
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" >
            
                <span>
            
                    
                    附录
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="dart2.14.html">
            
                <a href="dart2.14.html">
            
                    
                    升级到Dart 2.14
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="http://www.bczl.xyz/" target="blank" class="gitbook-link">
            编程之路
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >爬虫实战</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x722C;&#x866B;&#x5B9E;&#x6218;"><b></b>&#x722C;&#x866B;&#x5B9E;&#x6218;</a></li><ul><li><span class="title-icon "></span><a href="#&#x73AF;&#x5883;&#x51C6;&#x5907;"><b></b>&#x73AF;&#x5883;&#x51C6;&#x5907;</a></li><li><span class="title-icon "></span><a href="#&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;"><b></b>&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;</a></li><li><span class="title-icon "></span><a href="#&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;"><b></b>&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;</a></li><li><span class="title-icon "></span><a href="#&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;"><b></b>&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;</a></li></ul></ul></div><a href="#&#x722C;&#x866B;&#x5B9E;&#x6218;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><!-- ex_nolevel -->
<h1 id="&#x722C;&#x866B;&#x5B9E;&#x6218;"><a name="&#x722C;&#x866B;&#x5B9E;&#x6218;" class="anchor-navigation-ex-anchor" href="#&#x722C;&#x866B;&#x5B9E;&#x6218;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x722C;&#x866B;&#x5B9E;&#x6218;</h1>
<h2 id="&#x73AF;&#x5883;&#x51C6;&#x5907;"><a name="&#x73AF;&#x5883;&#x51C6;&#x5907;" class="anchor-navigation-ex-anchor" href="#&#x73AF;&#x5883;&#x51C6;&#x5907;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x73AF;&#x5883;&#x51C6;&#x5907;</h2>
<p>&#x6211;&#x4EEC;&#x5B8C;&#x5168;&#x624B;&#x52A8;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;<code>Dart</code>&#x5DE5;&#x7A0B;&#x8FD8;&#x662F;&#x7565;&#x663E;&#x9EBB;&#x70E6;&#xFF0C;&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x4E00;&#x4E2A;&#x811A;&#x624B;&#x67B6;&#xFF0C;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x5408;&#x4E4E;&#x89C4;&#x8303;&#x7684;<code>Dart</code>&#x5DE5;&#x7A0B;&#x9879;&#x76EE;&#xFF0C;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x5B89;&#x88C5;<del><code>stagehand</code></del>(&#x8FC7;&#x65F6;&#xFF0C;&#x53C2;&#x8003;<a href="dart2.14.html">&#x5347;&#x7EA7;&#x5230;Dart 2.14</a>)</p>
<pre><code class="lang-shell">pub global activate stagehand
</code></pre>
<p><del>&#x5B8C;&#x6210;&#x5B89;&#x88C5;&#x540E;&#x76F4;&#x63A5;&#x4F7F;&#x7528;<code>stagehand</code>&#x547D;&#x4EE4;&#xFF1A;<code>stagehand -h</code>&#x53EF;&#x80FD;&#x4F1A;&#x62A5;&#x627E;&#x4E0D;&#x5230;&#x9519;&#x8BEF;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x6211;&#x4EEC;&#x6709;&#x4E24;&#x79CD;&#x529E;&#x6CD5;&#x89E3;&#x51B3;</del></p>
<ol>
<li><p>&#x914D;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;</p>
<p> &#x6253;&#x5F00;<code>cmd</code>&#x547D;&#x4EE4;&#x884C;&#xFF0C;&#x8F93;&#x5165;&#x5982;&#x4E0B;&#x547D;&#x4EE4;</p>
<pre><code class="lang-shell"> echo %APPDATA%\Pub\Cache\bin
</code></pre>
<p> &#x8FD9;&#x65F6;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#xFF0C;&#x547D;&#x4EE4;&#x884C;&#x8F93;&#x51FA;&#x4E86;<code>stagehand</code>&#x547D;&#x4EE4;&#x6240;&#x5728;&#x7684;&#x8DEF;&#x5F84;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x5C06;&#x8BE5;&#x8DEF;&#x5F84;&#x52A0;&#x5165;&#x5230;&#x7CFB;&#x7EDF;&#x7684;<code>Path</code>&#x73AF;&#x5883;&#x53D8;&#x91CF;&#x5373;&#x53EF;</p>
</li>
<li><p>&#x4F7F;&#x7528;<code>pub</code>&#x5DE5;&#x5177;&#x8C03;&#x7528;</p>
<p> &#x9664;&#x4E86;&#x914D;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>pub global run</code>&#x53BB;&#x8C03;&#x7528;&#xFF0C;&#x7531;&#x4E8E;&#x6211;&#x672C;&#x673A;&#x914D;&#x7F6E;&#x4E86;&#x5404;&#x79CD;&#x5404;&#x6837;&#x7684;&#x5F00;&#x53D1;&#x8BED;&#x8A00;&#x548C;&#x5DE5;&#x5177;&#xFF0C;&#x547D;&#x4EE4;&#x5B9E;&#x5728;&#x592A;&#x591A;&#xFF0C;&#x6211;&#x5DF2;&#x7ECF;&#x4E0D;&#x592A;&#x559C;&#x6B22;&#x914D;&#x7F6E;&#x73AF;&#x5883;&#x53D8;&#x91CF;&#xFF0C;&#x8FD9;&#x91CC;&#x5C31;&#x5148;&#x4F7F;&#x7528;&#x8BE5;&#x65B9;&#x5F0F;&#x6F14;&#x793A;&#x3002;&#x6267;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x4E00;&#x4E0B;&#x5E2E;&#x52A9;</p>
<pre><code class="lang-shell"> pub global run stagehand -h
</code></pre>
</li>
</ol>
<h2 id="&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;"><a name="&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;" class="anchor-navigation-ex-anchor" href="#&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x521B;&#x5EFA;&#x5DE5;&#x7A0B;</h2>
<p>&#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x6587;&#x4EF6;&#x5939;<code>spider</code>&#xFF0C;<code>cd</code>&#x5230;&#x8BE5;&#x76EE;&#x5F55;&#x4E0B;&#xFF0C;&#x8FD0;&#x884C;&#x4EE5;&#x4E0B;&#x547D;&#x4EE4;&#xFF0C;&#x4F1A;&#x5728;<code>spider</code>&#x4E0B;&#x81EA;&#x52A8;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x547D;&#x4EE4;&#x884C;&#x9879;&#x76EE;</p>
<pre><code class="lang-shell">pub global run stagehand console-full
</code></pre>
<p>&#x4F7F;&#x7528;<code>vscode</code>&#x6253;&#x5F00;&#x8BE5;&#x9879;&#x76EE;&#x76EE;&#x5F55;</p>
<p>&#x7F16;&#x8F91;&#x914D;&#x7F6E;&#x6587;&#x4EF6;<code>pubspec.yaml</code>&#xFF0C;&#x907F;&#x514D;&#x4E0D;&#x5FC5;&#x8981;&#x7684;&#x4E0B;&#x8F7D;&#xFF0C;&#x5220;&#x9664;&#x9ED8;&#x8BA4;&#x6DFB;&#x52A0;&#x7684;<code>test</code>&#x5E93;&#x4F9D;&#x8D56;&#xFF0C;&#x914D;&#x7F6E;&#x5982;&#x4E0B;&#x4F9D;&#x8D56;&#x5E93;</p>
<pre><code class="lang-shell">dependencies:
  http: ^0.12.0+2
  html: ^0.14.0+2
</code></pre>
<p>&#x8FD9;&#x91CC;<code>http</code>&#x5E93;&#x4E3B;&#x8981;&#x7528;&#x4E8E;&#x5904;&#x7406;http&#x8BF7;&#x6C42;&#xFF0C;html&#x5E93;&#x7528;&#x4E8E;&#x5904;&#x7406;<code>html</code>&#x5185;&#x5BB9;&#x7684;&#x89E3;&#x6790;&#x4E0E;&#x63D0;&#x53D6;&#xFF0C;&#x5B83;&#x4EEC;&#x90FD;&#x662F;Dart&#x5B98;&#x65B9;&#x63D0;&#x4F9B;&#x7684;&#x975E;&#x6807;&#x51C6;&#x5E93;&#xFF0C;GitHub&#x94FE;&#x63A5;&#x5982;&#x4E0B;</p>
<ul>
<li><a href="https://github.com/dart-lang/http" target="_blank">http &#x5E93;&#x94FE;&#x63A5;</a></li>
<li><a href="https://github.com/dart-lang/html" target="_blank">html &#x5E93;&#x94FE;&#x63A5;</a></li>
</ul>
<p>&#x5728;&#x9879;&#x76EE;&#x4E0B;&#x6267;&#x884C;&#x547D;&#x4EE4;&#xFF0C;&#x4E0B;&#x8F7D;&#x4F9D;&#x8D56;</p>
<pre><code class="lang-shell">pub get
</code></pre>
<h2 id="&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;"><a name="&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;" class="anchor-navigation-ex-anchor" href="#&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6570;&#x636E;&#x7684;&#x83B7;&#x53D6;&#x4E0E;&#x89E3;&#x6790;</h2>
<p>&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;</p>
<pre><code class="lang-dart">  <span class="hljs-comment">// &#x8BF7;&#x6C42;&#x7F51;&#x7EDC;&#x6570;&#x636E;</span>
  Future&lt;<span class="hljs-built_in">String</span>&gt; _requestData(<span class="hljs-built_in">String</span> url) <span class="hljs-keyword">async</span> {
    <span class="hljs-keyword">var</span> response = <span class="hljs-keyword">await</span> http.<span class="hljs-keyword">get</span>(url, headers: header);
    <span class="hljs-keyword">if</span> (response.statusCode == <span class="hljs-number">200</span>) {
      <span class="hljs-keyword">return</span> response.body;
    }
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos;&lt;html&gt;error! status:<span class="hljs-subst">${response.statusCode}</span>&lt;/html&gt;&apos;</span>;
  }
</code></pre>
<p>&#x89E3;&#x6790;&#x6807;&#x7B7E;</p>
<pre><code class="lang-dart">  <span class="hljs-comment">// &#x89E3;&#x6790;&#x6570;&#x636E;</span>
  <span class="hljs-built_in">List</span>&lt;ItemModel&gt; _htmlParse(<span class="hljs-built_in">String</span> html) {
    Document <span class="hljs-built_in">document</span> = parse(html);

    <span class="hljs-built_in">List</span> contents = &lt;<span class="hljs-built_in">Element</span>&gt;[];
    <span class="hljs-built_in">List</span> images = &lt;<span class="hljs-built_in">Element</span>&gt;[];
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; parent = <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&apos;div.j-r-list-c&apos;</span>);

    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> e <span class="hljs-keyword">in</span> parent) {
      <span class="hljs-keyword">var</span> text = e.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&quot;div.j-r-list-c-desc &gt; a&quot;</span>);
      <span class="hljs-keyword">var</span> image = e.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&quot;div.j-r-list-c-img &gt; a &gt; img&quot;</span>);

      contents.add(text[<span class="hljs-number">0</span>]);
      images.add(image.isNotEmpty ? image[<span class="hljs-number">0</span>] : <span class="hljs-built_in">Element</span>.tag(<span class="hljs-string">&apos;img&apos;</span>));
    }

    <span class="hljs-comment"><span class="markdown">/// &#x8FD9;&#x91CC;&#x4F7F;&#x7528;css&#x9009;&#x62E9;&#x5668;&#x8BED;&#x6CD5;&#x63D0;&#x53D6;&#x6570;&#x636E;</span></span>
    <span class="hljs-comment">// &#x6587;&#x672C;</span>
    <span class="hljs-comment">// List&lt;Element&gt; contents = document.querySelectorAll(</span>
    <span class="hljs-comment">//     &apos;div[class=&quot;j-r-list&quot;] &gt; ul &gt; li &gt; div &gt; div[class=&quot;j-r-list-c-desc&quot;] &gt; a&apos;);</span>

    <span class="hljs-comment">// // &#x56FE;&#x7247;</span>
    <span class="hljs-comment">// List&lt;Element&gt; images = document.querySelectorAll(</span>
    <span class="hljs-comment">//     &apos;div[class=&quot;j-r-list&quot;] &gt; ul &gt; li &gt; div &gt; div[class=&quot;j-r-list-c-img&quot;] &gt; a &gt; img&apos;);</span>

    <span class="hljs-comment">// &#x70B9;&#x8D5E;</span>
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; likes = <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(
        <span class="hljs-string">&apos;div.j-r-list-tool &gt; div.j-r-list-tool-l &gt; ul &gt; li.j-r-list-tool-l-up &gt; span&apos;</span>);
    <span class="hljs-comment">// &#x70B9;&#x8E29;</span>
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; taps = <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(
        <span class="hljs-string">&apos;div.j-r-list-tool &gt; div.j-r-list-tool-l &gt; ul &gt; li.j-r-list-tool-l-down &gt; span&apos;</span>);

    <span class="hljs-comment">// &#x5934;&#x50CF;</span>
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; avatar =
        <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&apos;div.j-list-user &gt; div.u-img &gt; a &gt; img&apos;</span>);

    <span class="hljs-comment">// &#x6635;&#x79F0;</span>
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; nickNames =
        <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&apos;div.j-list-user &gt; div.u-txt &gt; a&apos;</span>);

    <span class="hljs-comment">// &#x65E5;&#x671F;</span>
    <span class="hljs-built_in">List</span>&lt;<span class="hljs-built_in">Element</span>&gt; date =
        <span class="hljs-built_in">document</span>.<span class="hljs-built_in">querySelectorAll</span>(<span class="hljs-string">&apos;div.j-list-user &gt; div.u-txt &gt; span&apos;</span>);

    <span class="hljs-built_in">List</span> data = <span class="hljs-built_in">List</span>&lt;ItemModel&gt;();
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; contents.length; i++) {
      ItemModel model = ItemModel();
      model..avatarUrl = avatar[i].attributes[<span class="hljs-string">&apos;src&apos;</span>]
      ..nickName = nickNames[i].text.trim()
      ..content = contents[i].text.trim()
      ..imgUrl = images[i].attributes[<span class="hljs-string">&apos;src&apos;</span>] ?? <span class="hljs-string">&apos;&apos;</span>
      ..like = <span class="hljs-built_in">int</span>.parse(likes[i].text)
      ..tap = <span class="hljs-built_in">int</span>.parse(taps[i].text)
      ..date = date[i].text;

      data.add(model);
    }
    <span class="hljs-keyword">return</span> data;
  }
</code></pre>
<p>&#x8FD9;&#x91CC;&#x4F7F;&#x7528;&#x7684;&#x662F;<code>css</code>&#x8BED;&#x6CD5;&#x9009;&#x62E9;&#x5668;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x63D0;&#x53D6;&#x3002;&#x5173;&#x4E8E;<code>css</code>&#x9009;&#x62E9;&#x5668;&#x8BED;&#x6CD5;&#xFF0C;&#x53EF;&#x4EE5;&#x53C2;&#x8003;&#x672C;&#x4EBA;&#x4E00;&#x7BC7;&#x535A;&#x5BA2; <a href="https://blog.csdn.net/yingshukun/article/details/88901510" target="_blank">&#x94FE;&#x63A5;&#x5730;&#x5740;</a>&#x3002;&#x53E6;&#x5916;&#xFF0C;&#x5728;&#x89E3;&#x6790;&#x65F6;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x5BFC;&#x5305;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x4F1A;&#x5B58;&#x5728;&#x51B2;&#x7A81;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:http/http.dart&apos;</span> <span class="hljs-keyword">as</span> http;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:html/parser.dart&apos;</span> show parse;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:html/dom.dart&apos;</span>;
</code></pre>
<h2 id="&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;"><a name="&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;" class="anchor-navigation-ex-anchor" href="#&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;</h2>
<p>&#x672C;&#x8BFE;&#x7A0B;&#x5206;&#x522B;&#x4F7F;&#x7528;<code>sqlite3</code>&#x548C;<code>PostgreSQL</code>&#x4E24;&#x79CD;&#x6570;&#x636E;&#x5E93;&#x6F14;&#x793A;&#x3002;&#x82E5;&#x7F3A;&#x4E4F;&#x6570;&#x636E;&#x5E93;&#x57FA;&#x7840;&#xFF0C;&#x6216;&#x4E0D;&#x4E86;&#x89E3;<code>sqlite3</code>&#xFF0C;&#x53EF;&#x5B66;&#x4E60;&#x672C;&#x4EBA;&#x7684;&#x4E00;&#x7BC7;&#x535A;&#x5BA2;&#x4F5C;&#x4E3A;&#x5165;&#x95E8; <a href="https://blog.csdn.net/yingshukun/article/details/94005900" target="_blank">&#x94FE;&#x63A5;&#x5730;&#x5740;</a></p>
<p>&#x5728;Dart&#x4E2D;&#x4F7F;&#x7528;<code>sqlite3</code>&#x9700;&#x8981;&#x5B89;&#x88C5;&#x76F8;&#x5E94;&#x7684;&#x7B2C;&#x4E09;&#x65B9;&#x5E93;&#xFF0C;&#x8FD9;&#x91CC;&#x63A8;&#x8350;&#x4F7F;&#x7528; <a href="https://pub.dev/packages/moor_ffi#-readme-tab-" target="_blank">moor_ffi</a>&#x3002;<code>PostgreSQL</code>&#x4EA6;&#x9700;&#x8981;&#x5B89;&#x88C5;&#x76F8;&#x5E94;&#x7684;&#x9A71;&#x52A8;  <a href="https://pub.dev/packages/postgresql2#-readme-tab-" target="_blank">postgresql2</a></p>
<p><code>sqlite3</code>&#x793A;&#x4F8B;</p>
<pre><code class="lang-dart"><span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:ffi&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;dart:io&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:moor_ffi/open_helper.dart&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;package:moor_ffi/database.dart&apos;</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">&apos;model.dart&apos;</span>;

<span class="hljs-keyword">const</span> _createTable = <span class="hljs-string">&apos;&apos;&apos; 
create table if not exists paragraph (
  id integer primary key autoincrement,
  nick_name text,
  avatar text,
  content text,
  img_url text,
  like_no integer,
  tap_no integer,
  date text
);
&apos;&apos;&apos;</span>;

<span class="hljs-comment">// &#x914D;&#x7F6E;sqlite3&#x7684;&#x52A0;&#x8F7D;&#x8DEF;&#x5F84;</span>
DynamicLibrary _openOnWin() {
  <span class="hljs-keyword">final</span> script = File(Platform.script.toFilePath()).parent;
  <span class="hljs-keyword">return</span> DynamicLibrary.open(<span class="hljs-string">&apos;<span class="hljs-subst">${script.path}</span>/sqlite3.dll&apos;</span>);
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DbTools</span> </span>{
  Database _db;

  DbTools(){
    open.overrideFor(OperatingSystem.windows, _openOnWin);
    _db = Database.open(<span class="hljs-string">&apos;db/data.db&apos;</span>);
    _db.execute(_createTable);
  }
}
</code></pre>
<p><code>PostgreSql</code> &#x793A;&#x4F8B;</p>
<p>&#x5173;&#x4E8E;<code>PostgreSql</code>&#x6570;&#x636E;&#x5E93;&#x7684;&#x5B89;&#x88C5;&#x53C2;&#x89C1;&#x4E0B;&#x4E00;&#x8282;</p>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PostgresDb</span></span>{
  <span class="hljs-keyword">static</span> <span class="hljs-keyword">const</span> uri = <span class="hljs-string">&apos;postgres://postgres:123456@localhost:5432/test_db&apos;</span>;
  Connection _conn;
  <span class="hljs-keyword">final</span> Completer _completer = Completer();

  PostgresDb(){
     _init();
  }

  <span class="hljs-keyword">void</span> _init() <span class="hljs-keyword">async</span>{
    _conn = <span class="hljs-keyword">await</span> connect(uri);
    <span class="hljs-keyword">await</span> _conn.execute(_createTable2);
    _completer.complete(<span class="hljs-keyword">true</span>);
  }

  <span class="hljs-keyword">void</span> saveAll(<span class="hljs-built_in">List</span>&lt;ItemModel&gt; data) <span class="hljs-keyword">async</span>{
    <span class="hljs-comment">// &#x7B49;&#x5F85;&#x521D;&#x59CB;&#x5316;&#x5B8C;&#x6210;</span>
    <span class="hljs-keyword">await</span> _completer.future;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> e <span class="hljs-keyword">in</span> data) {
      <span class="hljs-keyword">var</span> value = [e.nickName,e.avatarUrl,e.content,e.imgUrl,e.like,e.tap,e.date];
      <span class="hljs-keyword">await</span> _conn.execute(<span class="hljs-string">&apos;insert into paragraph (nick_name,avatar,content,img_url,like_no,tap_no,date)&apos;</span>
       <span class="hljs-string">&apos;values (@0,@1,@2,@3,@4,@5,@6)&apos;</span>,value);
    }
  }

  <span class="hljs-keyword">void</span> close()<span class="hljs-keyword">async</span>{
    <span class="hljs-keyword">await</span> _completer.future;
    _conn.close();
  }
}
</code></pre>
<hr>
<p><strong>&#x516C;&#x4F17;&#x53F7;&#x201C;&#x7F16;&#x7A0B;&#x4E4B;&#x8DEF;&#x4ECE;0&#x5230;1&#x201D;</strong></p>
<p><img src="https://gitee.com/arcticfox1919/ImageHosting/raw/master/img/20190301102949549.jpg" alt="20190301102949549"> </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; Arcticfox 2020 all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6587;&#x6863;&#x4FEE;&#x8BA2;&#x4E8E;&#xFF1A;
2021-10-18 21:06:00
</span></footer>
<script>console.log("plugin-popup....");document.onclick = function(e){ e.target.tagName === "IMG" && window.open(e.target.src,e.target.src)}</script><style>img{cursor:pointer}</style>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="网络编程.html" class="navigation navigation-prev " aria-label="Previous page: 网络编程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="HTTP服务器.html" class="navigation navigation-next " aria-label="Next page: HTTP服务器">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"爬虫实战","level":"1.5.2","depth":2,"next":{"title":"HTTP服务器","level":"1.5.3","depth":2,"path":"HTTP服务器.md","ref":"HTTP服务器.md","articles":[]},"previous":{"title":"网络编程","level":"1.5.1","depth":2,"path":"网络编程.md","ref":"网络编程.md","articles":[]},"dir":"ltr"},"config":{"plugins":["anchor-navigation-ex","expandable-chapters-small","-lunr","-search","search-pro","-sharing","github","expandable-chapters","pageview-count","theme-comscore","tbfed-pagefooter","popup","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy Arcticfox 2020","modify_label":"文档修订于：","modify_format":"YYYY-MM-DD HH:mm:ss"},"github":{"url":"https://github.com/arcticfox1919"},"search-pro":{},"popup":{},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":true},"favicon":"logo.ico","theme-comscore":{},"pageview-count":{},"custom-favicon":{},"expandable-chapters-small":{},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"expandable-chapters":{}},"theme":"default","author":"arcticfox","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Dart编程指南","language":"zh-hans","links":{"sidebar":{"主页":"http://www.bczl.xyz/"}},"gitbook":"*"},"file":{"path":"爬虫实战.md","mtime":"2021-10-18T13:06:00.122Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-10-18T14:58:03.414Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

